#include "main_window.h"
#include <gdk/gdkkeysyms.h>
#include "file_list_view.h"

static void on_destroy (GtkWidget* widget, gpointer data)
{
	gtk_main_quit();
}

static gboolean tmp;

static gboolean on_key_press_event (GtkWidget*		widget,
									GdkEventKey*	event,
									gpointer     	data) 
{
	MainWindow* mw;
	
	mw = (MainWindow*)data;
	
	if (event->type == GDK_KEY_PRESS && event->keyval == GDK_Tab)
	{
		g_signal_emit_by_name (G_OBJECT(mw->hpaned), "cycle-child-focus", TRUE, &tmp);
		return TRUE; // stop handling this event
	}
	
	return FALSE;
}

static GtkWidget* create_tab_panel()
{
	GtkWidget* 		tab_panel;
	GtkWidget* 		file_list_view1;
	GtkWidget* 		label1;
	GtkWidget* 		file_list_view2;
	GtkWidget* 		label2;
	GtkWidget*		scrolled1;
	GtkWidget*		scrolled2;
	const gchar*	DEFAULT_DIR = "c:\\";
	       
	tab_panel = gtk_notebook_new ();
	
	file_list_view1 = file_list_view_new (DEFAULT_DIR);
	scrolled1 = gtk_scrolled_window_new (NULL, NULL);
	gtk_container_add (GTK_CONTAINER(scrolled1), file_list_view1);
	label1 = gtk_label_new (DEFAULT_DIR);
	gtk_notebook_append_page (GTK_NOTEBOOK(tab_panel), scrolled1, label1);

	file_list_view2 = file_list_view_new (DEFAULT_DIR);
	scrolled2 = gtk_scrolled_window_new (NULL, NULL);
	gtk_container_add (GTK_CONTAINER(scrolled2), file_list_view2);
	label2 = gtk_label_new (DEFAULT_DIR);
	gtk_notebook_append_page (GTK_NOTEBOOK(tab_panel), scrolled2, label2);

	return tab_panel;
}

MainWindow* main_window_new ()
{
	MainWindow* mw;
	/*GtkWidget* 	hpaned;*/
	GtkWidget*	frame1;
	GtkWidget*	frame2;
	
	mw = g_new (MainWindow, 1);

	mw->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	g_signal_connect (G_OBJECT(mw->window), "destroy", G_CALLBACK(on_destroy), NULL);
	g_signal_connect (G_OBJECT(mw->window), "key-press-event", G_CALLBACK(on_key_press_event), mw);
	gtk_container_set_border_width (GTK_CONTAINER(mw->window), 10);
	gtk_window_set_title (GTK_WINDOW(mw->window), "File Manager");
	
	/* The Paned Window */
	mw->hpaned = gtk_hpaned_new();
	frame1 = gtk_frame_new (NULL);
	frame2 = gtk_frame_new (NULL);
	gtk_frame_set_shadow_type (GTK_FRAME(frame1), GTK_SHADOW_IN);
	gtk_frame_set_shadow_type (GTK_FRAME(frame2), GTK_SHADOW_IN);
	mw->tab_panel1 = create_tab_panel();
	mw->tab_panel2 = create_tab_panel();
	gtk_container_add (GTK_CONTAINER(frame1), mw->tab_panel1);
	gtk_container_add (GTK_CONTAINER(frame2), mw->tab_panel2);
	gtk_paned_add1 (GTK_PANED(mw->hpaned), frame1);
	gtk_paned_add2 (GTK_PANED(mw->hpaned), frame2);
	gtk_container_add (GTK_CONTAINER(mw->window), mw->hpaned);
		
	return mw;
}

void main_window_show (MainWindow* mw)
{
	gtk_widget_show_all (mw->window);
	gtk_window_resize (GTK_WINDOW(mw->window), 800, 600);
	gtk_paned_set_position (GTK_PANED(mw->hpaned), 400/*hpaned->allocation.width / 2*/);
}
